package com.hk.jmm.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import com.hk.jmm.AppApplication;
import com.hk.jmm.db.UserInfo;
import com.hk.jmm.helper.UserHelper;
import com.skyz.wrap.manager.TokenManager;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
    private static String uid;

    public static String getTableId() {
        if (TextUtils.isEmpty(uid)) {
            String userName = TokenManager.getInstance().getUserName(AppApplication.getApplication());
            UserInfo user = UserHelper.getInstance().getUserInfoByUserName(userName);
            if (user != null) {
                uid = user.getUserId();
            }
        }
        return uid + new Date().getTime();
    }

    //工具类
    public static String formatTime(Date date, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public static String formatNowTime() {
        Date date = new Date();
        String formatter = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public static void enableViews(View v, boolean enabled) {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                enableViews(vg.getChildAt(i), enabled);
            }
        }
        v.setEnabled(enabled);
    }

    public static void cleanFocus(Activity activity){
        Window window = activity.getWindow();
        View view = window.getCurrentFocus();
        if(view!=null){
            view.clearFocus();
        }
    }

    public static void cleanFocusKeyboardHid(Activity activity){
        Window window = activity.getWindow();
        View view = window.getCurrentFocus();
        if(view!=null){
            InputMethodManager imm = (InputMethodManager)activity. getSystemService(Context.INPUT_METHOD_SERVICE);
            boolean isKeyboardShowing = imm.isActive();
            if(isKeyboardShowing){
                view.clearFocus();
            }
        }
    }

    public static void cleanFocus(Activity activity,View view){
        cleanFocus(activity);
        if(view!=null){
            view.requestFocus();
        }
    }

    public static void hideSoftKeyboard(Activity activity) {
        // 步骤一：获取当前焦点的View
        View view = activity.getCurrentFocus();
        if(view!=null){
            // 步骤二：创建InputMethodManager对象
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            // 步骤三：关闭软键盘
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static void hideSoftKeyboard(Activity activity,View view) {
        if(view!=null){
            // 步骤二：创建InputMethodManager对象
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            // 步骤三：关闭软键盘
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static boolean softKeyboardShow(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm.isActive();
    }


}
